package com.yiligi.myserver.service.Impl;

import cn.hutool.core.util.StrUtil;
import com.yiligi.myserver.mapper.UserMapper;
import com.yiligi.myserver.pojo.DTO.ChangeUserPasswordDTO;
import com.yiligi.myserver.pojo.DTO.UserDTO;
import com.yiligi.myserver.pojo.User;
import com.yiligi.myserver.service.UserService;
import com.yiligi.myserver.util.TokenUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public List<User> selectAllUser() {
        return userMapper.selectAllUser();
    }

    @Override
    public User selectUserById(int id) {
        return userMapper.selectUserById(id);
    }

    @Override
    public int insertUser(User user) {
        return userMapper.insertUser(user);
    }

    @Override
    public int updateUser(User user) {
        return userMapper.updateUser(user);
    }

    @Override
    public Integer deleteUserById(Integer id) {
        return userMapper.deleteUserById(id);
    }

    /**
     * 分页查询
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public List<User> selectPage(int pageNum, int pageSize, String username, String email, String address) {
        pageNum = (pageNum-1) * pageSize;
        return userMapper.selectPage(pageNum,pageSize,username,email,address);
    }

    @Override
    public Integer selectTotal(String username, String email, String address) {
        return userMapper.selectTotal(username, email, address);
    }

    /**
     * 批量插入
     *
     * @param users
     * @return
     */
    @Override
    public Boolean addBatchUser(List<User> users) {
        return userMapper.addBatchUser(users);
    }

    @Override
    public UserDTO login(UserDTO userDTO) {
        UserDTO user =  userMapper.login(userDTO);
        if (user.getPassword().equals(userDTO.getPassword())){
            String token = TokenUtils.genToken(user.getUsername(), user.getPassword());
            user.setToken(token);
            return user;
        }
        return null;
    }

    /**
     * 验证用户名是否存在
     * @param username
     * @return
     */
    @Override
    public Boolean valUserName(String username) {
        return !StrUtil.isBlank(userMapper.valUserName(username));
    }

    @Override
    public int registerUser(UserDTO userDTO) {
        User user = new User();
        user.setUsername(userDTO.getUsername());
        user.setPassword(userDTO.getPassword());
        user.setNickname(userDTO.getNickname());
        return userMapper.registerUser(user);
    }

    @Override
    public User selectUserByName(String username) {
        return userMapper.selectUserByName(username);
    }

    @Override
    public Boolean changeUserPassword(ChangeUserPasswordDTO user) {
        if (Objects.equals(userMapper.selectUserPassword(user.getUsername()), user.getPassword())){
            return userMapper.changeUserPassword(user);
        }
        return false;
    }

}
